package edu.fmi.st.epf.types.team.ui.extension;

import java.util.Iterator;
import java.util.Map;

import org.eclipse.epf.authoring.ui.forms.CustomCategoryAssignPage;
import org.eclipse.epf.authoring.ui.forms.CustomCategoryDescriptionPage;
import org.eclipse.epf.authoring.ui.providers.IMethodElementEditorPageProviderExtension;
import org.eclipse.ui.forms.editor.FormEditor;

import edu.fmi.st.epf.types.team.common.util.TeamHelper;
import edu.fmi.st.epf.types.team.ui.pages.TeamDescriptionPage;
import edu.fmi.st.epf.types.team.ui.pages.TeamProjectMembersPage;
import edu.fmi.st.epf.types.team.ui.pages.TeamValuesPage;

public class TeamPageProviderExtension implements
		IMethodElementEditorPageProviderExtension {

	@Override
	public Map<Object, String> getPages(Map<Object, String> pageMap,
			FormEditor editor, Object input) {
		if (TeamHelper.isTeam(input)) {
			removeDescriptionPage(pageMap);
			removeAssignPage(pageMap);

			pageMap.put(new TeamDescriptionPage(editor), null);

			pageMap.put(new TeamProjectMembersPage(editor), null);

			pageMap.put(new TeamValuesPage(editor), null);
		}
		return pageMap;
	}

	private void removeDescriptionPage(Map<Object, String> pageMap) {
		for (Iterator<Object> iterator = pageMap.keySet().iterator(); iterator
				.hasNext();) {
			final Object key = iterator.next();
			if (key instanceof CustomCategoryDescriptionPage) {
				iterator.remove();
				// we assume that only one CustomCategoryDescriptionPage is
				// added - so break in first match
				break;
			}
		}
	}

	private void removeAssignPage(Map<Object, String> pageMap) {
		for (Iterator<Object> iterator = pageMap.keySet().iterator(); iterator
				.hasNext();) {
			final Object key = iterator.next();
			if (key instanceof CustomCategoryAssignPage) {
				iterator.remove();
				// we assume that only one AssignPage is added - so break in
				// first match
				break;
			}
		}
	}
}
